/**
 * 
 */
package com.kexinde.wjifen.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kexinde.wjifen.model.Shop;
import com.kexinde.wjifen.model.Shopkeeper;
import com.kexinde.wjifen.service.IShopkeeperManager;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author vincent 商家信息管理
 */
@Component("Shopkeeper")
@Scope("prototype")
public class ShopkeeperAction extends ActionSupport implements RequestAware,
		SessionAware {
	/**
	 * 管理员信息管理
	 */
	private Shopkeeper shopkeeper;
	private Shop shop;
	private String validate;
	private String newPassword;
	private IShopkeeperManager shopkeeperManager;
	private Map<String, Object> request;
	private Map<String, Object> session;

	public String showAllShopkeepers() { // 显示所有管理员信息
		List<Shopkeeper> shopkeepers = shopkeeperManager
				.showAllShopkeepers(shop.getId());
		request.put("shopkeepers", shopkeepers);
		return "showallsuccess";
	}

	public String showShopkeeper() { // 显示管理员信息
		shopkeeper = (Shopkeeper) session.get("shopkeeper");
		shopkeeper = shopkeeperManager.showShopkeeper(shopkeeper.getUserName());
		request.put("shopkeeper", shopkeeper);
		return "showsuccess";
	}

	public String add() { // 增加管理员
		if (shopkeeperManager.add(shopkeeper))
			return "addsuccess";
		else
			return "addfail";
	}

	public String modifyTel() { // 修改管理员信息
		if (shopkeeperManager.modifyTel(shopkeeper.getUserName(),
				shopkeeper.getSelfPhone())) {
			shopkeeper = this.shopkeeperManager.showShopkeeper(shopkeeper
					.getUserName());
			session.put("shopkeeper", shopkeeper);
			request.put("shopkeeperMsg", "修改信息成功");
			return "modifytel";
		} else {
			request.put("shopkeeperMsg", "修改信息失败");
			return "modifytel";
		}

	}

	public String modifyPwd() {
		String oldPwd=shopkeeper.getPassword();
		shopkeeper=(Shopkeeper) session.get("shopkeeper");
		if (shopkeeperManager.modifyPwd(shopkeeper.getUserName(), newPassword,
				oldPwd)) {
			request.put("shopkeeperMsg", "修改密码成功");
			return "modifypwd";
		} else {
			request.put("shopkeeperMsg", "修改密码失败");
			return "modifypwd";
		}
	}

	public String delete() { // 删除管理员信息
		if (shopkeeperManager.delete(shopkeeper))
			return "deletesuccess";
		else
			return "deletefail";
	}

	public String login() { // 商家管理员登录
		if (shopkeeperManager.check(shopkeeper.getUserName(),
				shopkeeper.getPassword())) {
			shopkeeper = this.shopkeeperManager.showShopkeeper(shopkeeper
					.getUserName());
			session.put("shopkeeper", shopkeeper);
			return "loginsuccess";
		} else
			return "loginfail";
	}

	public String logout() { // 商家管理员退出
		session.remove("shopkeeper");
		return "logoutsuccess";
	}

	public String exist() { // 检查帐号是否存在
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public Shopkeeper getShopkeeper() {
		return shopkeeper;
	}

	public void setShopkeeper(Shopkeeper shopkeeper) {
		this.shopkeeper = shopkeeper;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public IShopkeeperManager getShopkeeperManager() {
		return shopkeeperManager;
	}

	@Resource
	public void setShopkeeperManager(IShopkeeperManager shopkeeperManager) {
		this.shopkeeperManager = shopkeeperManager;
	}

	public String getValidate() {
		return validate;
	}

	public void setValidate(String validate) {
		this.validate = validate;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
}
